package com.shop.front.service.store;

import com.shop.front.vo.store.StoreProductReplyCountVo;
import com.shop.domain.store.*;
import com.shop.domain.user.User;
import com.shop.front.dao.store.*;
import com.shop.front.dao.user.UserDao;
import com.shop.front.request.store.StoreProductReplyAddReq;
import com.shop.web.response.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional
public class StoreProductReplyService {

    @Autowired
    private StoreOrderCartInfoDao storeOrderCartInfoDao;
    @Autowired
    private StoreProductReplyDao storeProductReplyDao;
    @Autowired
    private StoreProductReplySQLDao storeProductReplySQLDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private StoreOrderDao storeOrderDao;

    public StoreProductReplyCountVo count(Long productId) {
        return storeProductReplySQLDao.count(productId);
    }

    public BaseResponse page(Long productId, Integer type) {
        Page<StoreProductReply> page = storeProductReplySQLDao.page(productId, type);
        return new BaseResponse(page);
    }

    public BaseResponse addReply(Long userId, StoreProductReplyAddReq req) {
        StoreProductReply productReply = storeProductReplyDao.findByUserIdAndOcid(userId, req.getOrderCartInfoId());
        if(null != productReply) {
            return BaseResponse.fail("您已经评论过该商品！");
        }

        StoreOrderCartInfo orderCartInfo = storeOrderCartInfoDao.findSingle(req.getOrderCartInfoId());

        User user = userDao.findSingle(userId);

        productReply = new StoreProductReply();
        productReply.setUid(userId);
        productReply.setProductId(orderCartInfo.getProductId());
        productReply.setOid(orderCartInfo.getOid());
        productReply.setOcid(orderCartInfo.getId());
        productReply.setComment(req.getComment());
        productReply.setPics(req.getPics());
        productReply.setProductScore(req.getProductScore());
        productReply.setServiceScore(req.getServiceScore());
        productReply.setNickname(user.getNickname());
        productReply.setAvatar(user.getAvatar());
        storeProductReplyDao.save(productReply);

        //评论之后修改订单状态未已完成
        storeOrderDao.updateStatusByDiscussFinish(orderCartInfo.getOid());

        return new BaseResponse();
    }
}
